﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;
using System.Collections.ObjectModel;
using System.Collections;

namespace BitPortal.Admin.Article.Article
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "ArticleView", useExceptionHandler: true);
            base.OnInit(e);
            (this.ArticleAddEdit1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
            (this.GridPanel1.SelectionModel.Primary as RowSelectionModel).ClearSelections();
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            ArticleAddEdit1.BindData(0);
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
            if (sm.SelectedRows.Count > 0)
            {
                long ID = Convert.ToInt64(sm.SelectedRows[0].RecordID);
                ArticleAddEdit1.BindData(ID);
            }

        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            List<Models.Article> data;
            if (Session["ArticleGridFlag"] != null && Session["ArticleGridFlag"].Equals(false))
            {
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                data = bitEnt.Articles.Where(ent => ent.IsDeleted == true).ToList();
            }
            else
            {
                data = ArticleBLL.GetAll().ToList();
            }
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            this.Store1.DataSource = ExtNet.GirdData.Filter<Models.Article>(data, fc, e);
        }

        protected void btnSetApproved_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Article article = bitEnt.Articles.SingleOrDefault(ent => ent.ArticleID == ID);
                    if (article != null)
                        article.IsApproved = rgApproved.Items[0].Checked;
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Cập nhật " + record + " bài viết thành công!");
                    LoadGridData();
                } 
                Window3.Hide();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnSetCategory_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Article article = bitEnt.Articles.SingleOrDefault(ent => ent.ArticleID == ID);
                    if (article != null)
                        article.CategoryID = Convert.ToInt32(CBArticleCategory2.ComboboxCategory.SelectedItem.Value);
                }
                int record = bitEnt.SaveChanges();
                Notification.Show(new NotificationConfig
                {
                    Title = "Thông báo",
                    Icon = Icon.Information,
                    Html = "Cập nhật " + record + " bài viết thành công!"
                });
                Window2.Hide();
                LoadGridData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnSetEnabled_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Article article = bitEnt.Articles.SingleOrDefault(ent => ent.ArticleID == ID);
                    if (article != null)
                        article.IsEnabled = rgEnabled.Items[0].Checked;
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Cập nhật " + record + " bài viết thành công!");
                    LoadGridData();
                }
                Window4.Hide();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnSetHot_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Article article = bitEnt.Articles.SingleOrDefault(ent => ent.ArticleID == ID);
                    if (article != null)
                        article.IsHot = rgHot.Items[0].Checked;
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Cập nhật " + record + " bài viết thành công!");
                    LoadGridData();
                }
                Window5.Hide();
                
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleDelete");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Article article = bitEnt.Articles.SingleOrDefault(ent => ent.ArticleID == ID);
                    if (article != null)
                    {
                        article.IsDeleted = true;
                    }
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " bài viết thành công!");
                    sm.ClearSelections();
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrash_Click(object sender, DirectEventArgs e)
        {
            if (Session["ArticleGridFlag"] != null && Session["ArticleGridFlag"].Equals(false))
            {
                Session["ArticleGridFlag"] = true;
            }
            else
            {
                Session["ArticleGridFlag"] = false;
            }
            LoadGridData();
        }

        protected void btnTrashEmpty_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleDelete");

                int record = 0;
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                foreach (BitPortal.Models.Article a in bitEnt.Articles.Where(ent => ent.IsDeleted == true).ToList())
                {
                    if (ArticleBLL.DeletePermanently(a.ArticleID) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " bài viết trong thùng rác thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrashDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleDelete");

                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (ArticleBLL.DeletePermanently(Convert.ToInt64(row.RecordID)) > 0)
                    {
                        record++;
                    }
                }
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " bài viết trong thùng rác thành công!");
                    sm.ClearSelections();
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnUnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ArticleEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.Article article = bitEnt.Articles.SingleOrDefault(ent => ent.ArticleID == ID);
                    if (article != null)
                        article.IsDeleted = false;
                }
                int record = bitEnt.SaveChanges();
                if (record > 0)
                {
                    ShowNotification("Xóa " + record + " bài viết thành công!");
                    LoadGridData();
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

    }
}